import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { UserConfig, ConfigEnv, loadEnv } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'

// https://vitejs.dev/config/
export default ({ command, mode }) => {
  const root = process.cwd()
  const env = loadEnv(mode, root)

  return {
    base: env.VITE_APP_BASE + '/',

    build: {
      outDir: `dist/admin`,
    },

    plugins: [
      vue(),
      AutoImport({
        imports: ['vue', 'vue-router', 'vue-i18n'],
      }),
    ],

    resolve: {
      alias: [
        { find: '@', replacement: path.resolve(__dirname, './src') },
        {
          find: 'jax-base',
          replacement: path.resolve(__dirname, './src/jax-base-vue'),
        },
        {
          find: 'jax-ui',
          replacement: path.resolve(__dirname, './src/jax-admin-vue/jax-ui'),
        },
        {
          find: 'jax-node',
          replacement: path.resolve(__dirname, './src/jax-admin-vue/jax-node'),
        },
      ],
    },

    // 代理请求
    server: {
      proxy: {
        [env.VITE_APP_API]: {
          target: env.VITE_APP_SERVER,
          changeOrigin: true,
          // rewrite: path => path.replace(/^\/api/, '')
        },
      },
    },
  }
}
